package com.example.shoppingcenter.Service;

import com.example.shoppingcenter.POJO.Result;
import com.example.shoppingcenter.POJO.User;
import com.example.shoppingcenter.Mapper.userMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class userService {
    @Autowired
    private userMapper userMapper;
    @Autowired
    private Result result;

    public User getUserByAccount(String account) {
        return userMapper.getUserByAccount(account);
    }

    public Result updateUser(User user) {
        int ans = userMapper.updateUser(user);
        Result result = new Result();
        if(ans > 0){
            result.setMessage("修改成功");
            result.setResult(true);
        }else {
            result.setMessage("修改失败");
            result.setResult(false);
        }
        return result;
    }

    public Result changePwd(String password, int userId) {
        int ans = userMapper.changePwd(password, userId);
        Result result = new Result();
        if(ans > 0){
            result.setMessage("修改成功");
            result.setResult(true);
        }else {
            result.setMessage("修改失败");
            result.setResult(false);
        }
        return result;
    }

    public Result signOn(String account, String password) {
        User user = userMapper.getUserByAccountPassword(account, password);
        if (user != null) {
            result.setMessage("登录成功");
            result.setResult(true);
            return result;
        } else {
            result.setMessage("你的账号或密码有误，请再试一次");
            result.setResult(false);
            return result;
        }
    }

    public Result signUp(String account, String password, String repassword, String name) {
        User user = userMapper.GetUserByAccount(account);
        Result result = new Result();
        if (user != null) {
            result.setMessage("该账号已存在");
            result.setResult(false);
            return result;
        } else {
            userMapper.signUpData(account, password,name);
            result.setMessage("注册成功");
            result.setResult(true);
            return result;
        }
    }
}
